<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>mysql一主多从同步配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>mysql一主多从同步配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<h2 id="一、环境"><a href="#一、环境" class="headerlink" title="一、环境"></a>一、环境</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master：192.168.101<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MYSQL版本：5.1.48-community-log</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave1:192.168.2.182<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MYSQL版本：5.1.48-community-log</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave2:192.168.2.111<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MYSQL版本：5.1.48-community-log</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;so…1 vs 2。</p>
<h2 id="二、master和-slave上的相关配置"><a href="#二、master和-slave上的相关配置" class="headerlink" title="二、master和 slave上的相关配置"></a>二、master和 slave上的相关配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3台上都一样：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>在/etc目录下可能无my.cnf文件，从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost etc]<span class="comment"># cp /usr/share/mysql/my-medium.cnf my.cnf</span></div><div class="line">[root@localhost etc]<span class="comment"># ll |grep my</span></div><div class="line">-rwxr-xr-x 1 root root 5204 Feb 13 22:52 my_bak</div><div class="line">-rwxr-xr-x 1 root root 4765 Jul 10 23:07 my.cnf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master 上;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@mysql101 ~]<span class="comment"># vi /etc/my.cnf</span></div></pre></td></tr></table></figure>
<h3 id="1-修改master上的配置文件my-cnf。"><a href="#1-修改master上的配置文件my-cnf。" class="headerlink" title="1.修改master上的配置文件my.cnf。"></a>1.修改master上的配置文件my.cnf。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在[mysqld]下添加如下字段：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">server-id = 1</div><div class="line"><span class="built_in">log</span>-bin=mysql-bin</div><div class="line">binlog-do-db=YYY //需要同步的数据库</div><div class="line">binlog-ignore-db=mysql //被忽略的数据库</div><div class="line">binlog-ignore-db=information-schema //被忽略的数据库</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在master上为slave添加一个同步账号</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; grant replication slave on *.* to <span class="string">'affairlog'</span>@<span class="string">'192.168.2.182'</span> identified by <span class="string">'pwd123'</span>;</div><div class="line">//在slave1上登陆成功</div><div class="line">mysql&gt; grant replication slave on *.* to <span class="string">'affairlog'</span>@<span class="string">'192.168.2.111'</span> identified by <span class="string">'pwd123'</span>;</div><div class="line">//在slave2上登陆成功</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，重启master的mysql服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service mysql restart;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用show master status命令查看日志情况</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show master status\G;</div><div class="line">*************************** 1. row ***************************</div><div class="line">File: mysql-bin.000087</div><div class="line">Position: 106</div><div class="line">Binlog_Do_DB: YYY</div><div class="line">Binlog_Ignore_DB: mysql,information-schema</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<h3 id="2-修改slave1上的配置文件my-cnf。"><a href="#2-修改slave1上的配置文件my-cnf。" class="headerlink" title="2.修改slave1上的配置文件my.cnf。"></a>2.修改slave1上的配置文件my.cnf。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在[mysqld]下添加如下字段</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@mysql182 ~]<span class="comment"># vi /etc/my.cnf</span></div><div class="line">server-id=182</div><div class="line">master-host=192.168.3.101</div><div class="line">master-user= affairlog</div><div class="line">master-password=pwd123</div><div class="line">master-port=3306</div><div class="line">master-connect-retry=60</div><div class="line">replicate-do-db=YYY //同步的数据库</div><div class="line">replicate-ignore-db=mysql //被忽略的数据库</div><div class="line">replicate-ignore-db=information-schema //被忽略的数据库</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，重启slave的mysql服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service mysql restart;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改slave2上的配置文件my.cnf，和上面类似，只是把server-id改下，为了方便，我都用了相应的ip某位，</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以，slave2上我设置的server-id是111。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在进入两个slave机中的mysql。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div></pre></td><td class="code"><pre><div class="line">mysql&gt;start slave;</div><div class="line">mysql&gt;show slave status\G;</div><div class="line">*************************** 1. row ***************************</div><div class="line">Slave_IO_State: Waiting <span class="keyword">for</span> master to send event</div><div class="line">Master_Host: 192.168.3.101</div><div class="line">Master_User: affairlog</div><div class="line">Master_Port: 3306</div><div class="line">Connect_Retry: 60</div><div class="line">Master_Log_File: mysql-bin.000087</div><div class="line">Read_Master_Log_Pos: 106</div><div class="line">Relay_Log_File: vm111-relay-bin.000002</div><div class="line">Relay_Log_Pos: 251</div><div class="line">Relay_Master_Log_File: mysql-bin.000087</div><div class="line">Slave_IO_Running: Yes</div><div class="line">Slave_SQL_Running: Yes</div><div class="line">Replicate_Do_DB: YYY</div><div class="line">Replicate_Ignore_DB: mysql,information-schema</div><div class="line">Replicate_Do_Table:</div><div class="line">Replicate_Ignore_Table:</div><div class="line">Replicate_Wild_Do_Table:</div><div class="line">Replicate_Wild_Ignore_Table:</div><div class="line">Last_Errno: 0</div><div class="line">Last_Error:</div><div class="line">Skip_Counter: 0</div><div class="line">Exec_Master_Log_Pos: 106</div><div class="line">Relay_Log_Space: 406</div><div class="line">Until_Condition: None</div><div class="line">Until_Log_File:</div><div class="line">Until_Log_Pos: 0</div><div class="line">Master_SSL_Allowed: No</div><div class="line">Master_SSL_CA_File:</div><div class="line">Master_SSL_CA_Path:</div><div class="line">Master_SSL_Cert:</div><div class="line">Master_SSL_Cipher:</div><div class="line">Master_SSL_Key:</div><div class="line">Seconds_Behind_Master: 0</div><div class="line">Master_SSL_Verify_Server_Cert: No</div><div class="line">Last_IO_Errno: 0</div><div class="line">Last_IO_Error:</div><div class="line">Last_SQL_Errno: 0</div><div class="line">Last_SQL_Error:</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>如果两个slave中的Slave_IO_Running、Slave_SQL_Running状态均为Yes则表明设置成功。</strong></p>
<h2 id="三、可能遇到的问题："><a href="#三、可能遇到的问题：" class="headerlink" title="三、可能遇到的问题："></a>三、可能遇到的问题：</h2><h3 id="问题1：Slave-IO-Running-No或者Slave-SQL-Running-No"><a href="#问题1：Slave-IO-Running-No或者Slave-SQL-Running-No" class="headerlink" title="问题1：Slave_IO_Running: No或者Slave_SQL_Running: No"></a>问题1：Slave_IO_Running: No或者Slave_SQL_Running: No</h3><h4 id="停掉slave服务"><a href="#停掉slave服务" class="headerlink" title="停掉slave服务"></a>停掉slave服务</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; slave stop;</div><div class="line">Query OK, 0 rows affected (2.01 sec)</div></pre></td></tr></table></figure>
<h4 id="解决办法"><a href="#解决办法" class="headerlink" title="解决办法"></a>解决办法</h4><ol>
<li>在master上查看。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show master status\G;</div><div class="line">*************************** 1. row ***************************</div><div class="line">File: mysql-bin.000087</div><div class="line">Position: 1845</div><div class="line">Binlog_Do_DB: YYY</div><div class="line">Binlog_Ignore_DB: mysql,information-schema</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ol>
<li>到slave上手动同步。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt;change master to</div><div class="line">&gt;master_host=<span class="string">'192.168.3.101'</span>,</div><div class="line">&gt;master_user=<span class="string">'affairlog'</span>,</div><div class="line">&gt;master_password=<span class="string">'pwd123'</span>,</div><div class="line">&gt;master_log_file=<span class="string">'mysql-bin.000087'</span>,</div><div class="line">&gt;master_log_pos=1845;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<h4 id="启动slave服务"><a href="#启动slave服务" class="headerlink" title="启动slave服务"></a>启动slave服务</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; slave start;</div></pre></td></tr></table></figure>
<h4 id="再次查看Slave-IO-Running、Slave-SQL-Running状态，为Yes则表明设置成功。"><a href="#再次查看Slave-IO-Running、Slave-SQL-Running状态，为Yes则表明设置成功。" class="headerlink" title="再次查看Slave_IO_Running、Slave_SQL_Running状态，为Yes则表明设置成功。"></a>再次查看Slave_IO_Running、Slave_SQL_Running状态，为Yes则表明设置成功。</h4><h3 id="问题2：RROR-1198-HY000-This-operation-cannot-be-performed-with-a-running-slave-run-STOP-SLAVE-first"><a href="#问题2：RROR-1198-HY000-This-operation-cannot-be-performed-with-a-running-slave-run-STOP-SLAVE-first" class="headerlink" title="问题2：RROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first"></a>问题2：RROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误重现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt;change master to</div><div class="line">&gt;master_host=<span class="string">'192.168.3.101'</span>,</div><div class="line">&gt;master_user=<span class="string">'affairlog'</span>,</div><div class="line">&gt;master_password=<span class="string">'pwd123'</span>,</div><div class="line">&gt;master_log_file=<span class="string">'mysql-bin.000087'</span>,</div><div class="line">&gt;master_log_pos=1845;</div><div class="line">ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方法：</p>
<ol>
<li>停掉slave服务</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; slave stop;</div><div class="line">Query OK, 0 rows affected (2.01 sec)</div></pre></td></tr></table></figure>
<ol>
<li>重置slave服务</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; reset stop;</div><div class="line">Query OK, 0 rows affected (2.01 sec)</div></pre></td></tr></table></figure>
<ol>
<li>再执行一次change命令</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt;change master to</div><div class="line">&gt;master_host=<span class="string">'192.168.3.101'</span>,</div><div class="line">&gt;master_user=<span class="string">'affairlog'</span>,</div><div class="line">&gt;master_password=<span class="string">'pwd123'</span>,</div><div class="line">&gt;master_log_file=<span class="string">'mysql-bin.000087'</span>,</div><div class="line">&gt;master_log_pos=1845;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<ol>
<li>启动slave服务</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; slave start;</div></pre></td></tr></table></figure>
<p>5．再次查看Slave_IO_Running、Slave_SQL_Running状态，为Yes则表明设置成功。</p>
<h3 id="PS："><a href="#PS：" class="headerlink" title="PS："></a>PS：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Slave_IO_Running：连接到主库，并读取主库的日志到本地，生成本地日志文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Slave_SQL_Running:读取本地日志文件，并执行日志里的SQL命令。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/32. mysql一主多从同步配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
